package com.mdd.front.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayFundAuthOperationDetailQueryResponse;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.enums.ErrorEnum;
import com.mdd.front.service.IAuthOrderService;
import com.mdd.front.validate.agreement.UserAgreementSign;
import com.mdd.front.validate.freeze.FreezeInfo;
import com.mdd.front.vo.agreement.UserSignVo;
import com.mdd.front.vo.order.OrderIdValidate;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 *
 * {@code @Description}
 * {@code @Author} 幻速
 * {@code @Date} 2023/11/1 17:47
 **/
@RestController
@RequestMapping("/api/agreement")
@Api(tags = "用户支付管理")
public class UserAgreementController {
    @Resource
    IAuthOrderService iAuthOrderService;
    //发起押金冻结
    @PostMapping("/authFreeze")
    public AjaxResult<String> authOrderFreeze(@RequestBody OrderIdValidate orderIdValidate)   {
        return new AjaxResult<>(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg(), iAuthOrderService.authOrderFreeze(orderIdValidate)) ;
    }

    /**
     * 取消冻结押金
     * @param orderIdValidate
     * @return
     */
    @PostMapping("/cancelAuthFreeze")
    public AjaxResult<Object> cancelAuthFreeze(@RequestBody OrderIdValidate orderIdValidate) {
        try {
            iAuthOrderService.cancelAuthOrderFreeze(orderIdValidate.getOrderId());
            return  AjaxResult.success();
        }catch (AlipayApiException e){
            return AjaxResult.failed(e.getErrMsg());
        }
    }
    /**
     * 获取冻结押金详情
     * @return
     */
    @PostMapping("/queryAuthOrderFreeze")
    public AjaxResult<Object> queryAuthOrderFreeze(@RequestBody OrderIdValidate orderIdValidate) {
        try {
            AlipayFundAuthOperationDetailQueryResponse alipayFundAuthOperationDetailQueryResponse = iAuthOrderService.queryAuthOrderFreeze(orderIdValidate.getOrderId());
            return  AjaxResult.success(alipayFundAuthOperationDetailQueryResponse);
        }catch (AlipayApiException e){
            return AjaxResult.failed(e.getErrMsg());
        }
    }
    //代扣签约
    @PostMapping("/userAgreementSign")
    public AjaxResult<String> userAgreementPageSign(@RequestBody OrderIdValidate orderIdValidate)   {
        return new AjaxResult<>(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg(), iAuthOrderService.userAgreementPageSign(orderIdValidate)) ;
    }

    /**
     * 取消代扣签约
     * @param orderIdValidate
     * @return
     */
    @PostMapping("/cancelUserAgreementSign")
    public AjaxResult<Object> cancelUserAgreementSign(@RequestBody OrderIdValidate orderIdValidate) {
        try {
            iAuthOrderService.cancelUserAgreementRecord(orderIdValidate.getOrderId());
            return  AjaxResult.success();
        }catch (AlipayApiException e){
            return AjaxResult.failed(e.getErrMsg());
        }
    }

    //获取签约状态
    @PostMapping("/getUserSignInfo")
    public AjaxResult<UserSignVo> getUserSignInfo(@RequestBody OrderIdValidate orderIdValidate)   {
        return   AjaxResult.success(iAuthOrderService.getSignStatus(orderIdValidate));
    }
}
